package com.draft.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author Meng Xu
 * @implSpec
 * @date 2023-11-15 13:44
 **/
@Slf4j
@RequestMapping("/office")
@RestController
public class OfficeController {

    @GetMapping("/one")
    public void testOne() throws InterruptedException {

        log.info("test o  1111");

        Thread.sleep(10000);

        log.info("test o  2222");
    }


    @GetMapping("/test")
    public void test() throws Exception {

        try {

            boolean encrypted = isEncrypted("C:\\Users\\farben\\Desktop\\test.xlsx");

            init("C:\\Users\\farben\\Desktop\\qaq.docx");

            System.out.println(encrypted);

        } catch(EncryptedDocumentException e) {

            System.out.println("sdaasda111111");
            e.printStackTrace();
        }
    }

    public  boolean isEncrypted(String filePath) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(Files.newInputStream(Paths.get(filePath)));

//            SXSSFWorkbook workbook1 = new SXSSFWorkbook(workbook);

//            workbook1.close();
            workbook.close();
            return false; // 文件未加密
        } catch (EncryptedDocumentException e) {
            return true; // 文件加密
        } catch (OfficeXmlFileException e) {
            return false; // 文件未加密
        } catch (IOException e) {
            e.printStackTrace();
            return false; // 文件未加密
        }
    }



    public static boolean isEncrypted1(String filePath) {
        try {
            FileInputStream fis = new FileInputStream(filePath);
            POIFSFileSystem fs = new POIFSFileSystem(fis);
            DirectoryNode root = fs.getRoot();
//            DocumentInputStream dis = root.createDocumentInputStream("EncryptionInfo");
            EncryptionInfo info = new EncryptionInfo(root);
//            info.getVerifier()
//            EncryptionVerifier verifier = info.getVerifier();
//            boolean isEncrypted = verifier.verifyPassword(null);
//            dis.close();
            fis.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false; // 文件打开异常，认为未加密
        }
    }

    public void init(String path) throws Exception {
        FileInputStream inputStream = new FileInputStream(path);
        XSSFWorkbook xwpfDocument = new XSSFWorkbook(inputStream);

    }


}
